import tkinter as tk
from tkinter import scrolledtext
import pygame

class CalligraphyScroll:
    def __init__(self, master):
        self.master = master
        self.master.title("The one I love most")
        self.master.geometry("1000x800")  # 增大窗口尺寸
        self.master.configure(bg="#2E2E2E")

        # 初始化组件
        self.canvas = tk.Canvas(master, bg="#E6D9C3", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True)

        # 卷轴参数
        self.scroll_width = 50
        self.painting_width = 0
        self.target_width = 850  # 加宽画卷
        self.painting_height = 550  # 加高画卷
        self.animation_speed = 25

        # 初始化元素
        self.create_scroll_ends()
        self.create_painting()
        self.animate_unroll()

        # 初始化pygame mixer
        pygame.mixer.init()
        try:
            pygame.mixer.music.load(
                r'/情人节/assets/cd40_789c_4e77_e4d21eb140f2ecebf43ae2e68750111f.mp3')  # 请替换为实际的音乐文件路径
            pygame.mixer.music.set_volume(0.3)  # 设置音量
            pygame.mixer.music.play(-1)  # 循环播放音乐
        except Exception as e:
            print("音乐加载失败:", e)  # 打印异常信息

    def create_scroll_ends(self):
        # 调整卷轴位置适应新高度
        self.left_scroll = self.canvas.create_oval(50, 100, 100, 650, fill="#5B3C11", outline="#3D2B0E")
        self.right_scroll = self.canvas.create_oval(50, 100, 100, 650, fill="#5B3C11", outline="#3D2B0E")

    def create_painting(self):
        self.painting = self.canvas.create_rectangle(75, 100, 75, 650, fill="#F5F5DC", outline="#C0B8A0", width=2)

    def animate_unroll(self):
        if self.painting_width < self.target_width:
            self.painting_width += 10
            self.canvas.coords(self.painting, 75, 100, 75 + self.painting_width, 650)
            self.canvas.coords(self.right_scroll,
                               75 + self.painting_width - 25, 100,
                               75 + self.painting_width + 25, 650)
            self.master.after(30, self.animate_unroll)
        else:
            self.add_decorations()
            self.init_text_animation()

    def init_text_animation(self):
        # 加长示例文本
        self.text_content = """


    亲爱的姑娘，展信佳。

        我喜欢日落余晖后在田间地头吹完风  
         我喜欢暮色四合是望着天上的月亮  
          我喜欢夜阑人静时独自对酒当歌  
           我喜欢四更天时的鸡鸣犬吠声  
            我喜欢天空拂晓时的一点光  
             我喜欢清晨窗外鹊声穿林  
              我喜欢朝阳的光暖衣襟  
               我喜欢午间的一盏茶  
                我喜欢正午的微风  
                 我喜欢青涩时光  
                  我喜欢思念你  
                   我喜欢平淡  
                    我喜欢你  
                     我说我  
                      喜欢  
                       你

                 蒹葭苍苍，白露为霜
                 所谓伊人，在水一方

                                      2025年5月20日
        """
        self.current_index = 0
        self.is_animating = True

        # 创建带滚动条的文本容器
        self.text_frame = tk.Frame(self.canvas, bg="#F5F5DC")
        self.text_area = scrolledtext.ScrolledText(
            self.text_frame,
            wrap=tk.WORD,
            font=("楷体", 14),
            width=60,
            height=45,  # 增加显示行数
            bg="#F5F5DC",
            fg="#3D2B0E",
            padx=30,
            pady=30,
            highlightthickness=0,
            state=tk.DISABLED  # 初始禁用编辑
        )
        self.text_area.pack(expand=True, fill=tk.BOTH)

        # 定位文本容器
        self.canvas.create_window(
            75 + self.target_width / 2,
            375,  # 居中定位
            window=self.text_frame,
            width=self.target_width - 100,
            height=500  # 增大文本区域高度
        )

        self.animate_text()

    def animate_text(self):
        if self.current_index < len(self.text_content) and self.is_animating:
            # 临时启用编辑以插入文字
            self.text_area.configure(state=tk.NORMAL)

            next_char = self.text_content[self.current_index]
            self.text_area.insert(tk.END, next_char)
            self.text_area.see(tk.END)  # 自动滚动到底部
            self.text_area.configure(state=tk.DISABLED)

            # 调整标点符号停顿
            delay = self.animation_speed
            if next_char in ["，", "。", "？", "！"]:
                delay *= 3
            elif next_char == "\n":
                delay *= 2

            self.current_index += 1
            self.master.after(delay, self.animate_text)
        else:
            self.add_control_buttons()

    def add_control_buttons(self):
        btn_frame = tk.Frame(self.canvas, bg="#F5F5DC")

        tk.Button(
            btn_frame,
            text="🔄 重播",
            command=self.reset_animation,
            font=("楷体", 14),
            bg="#5B3C11",
            fg="white",
            relief="flat"
        ).pack(side=tk.LEFT, padx=10, pady=10)

        tk.Button(
            btn_frame,
            text="✍️ 续写",
            command=self.show_writing_pad,
            font=("楷体", 14),
            bg="#8B0000",
            fg="white",
            relief="flat"
        ).pack(side=tk.LEFT, padx=10, pady=10)

        self.canvas.create_window(500, 700, window=btn_frame)

    def reset_animation(self):
        self.is_animating = False
        self.text_area.configure(state=tk.NORMAL)
        self.text_area.delete("1.0", tk.END)
        self.text_area.configure(state=tk.DISABLED)
        self.current_index = 0
        self.is_animating = True
        self.animate_text()

    def show_writing_pad(self):
        self.text_area.configure(state=tk.NORMAL)
        self.text_area.focus_set()

        # 添加保存按钮
        tk.Button(
            self.canvas,
            text="📜 落款保存",
            command=self.save_content,
            font=("隶书", 16),
            bg="#8B0000",
            fg="white",
            relief="flat"
        ).place(x=800, y=700)

    def save_content(self):
        content = self.text_area.get("1.0", tk.END)
        print("内容已保存：\n", content)

        # 印章效果
        self.canvas.create_oval(820, 680, 880, 740, fill="#8B0000", outline="")
        self.canvas.create_text(850, 710,
                                text="存\n鉴",
                                font=("篆书", 14),
                                fill="white",
                                justify="center")

    def add_decorations(self):
        self.canvas.create_line(75, 100, 925, 100, fill="#5B3C11", width=3)
        self.canvas.create_line(75, 650, 925, 650, fill="#5B3C11", width=3)
        self.canvas.create_text(850, 150, text="❀", font=("楷体", 48), fill="#8B0000")


if __name__ == "__main__":
    root = tk.Tk()
    app = CalligraphyScroll(root)
    root.mainloop()



